package fr.ensisa.sunspot.automate;

import java.util.ArrayList;

import fr.ensisa.sunspot.event.IEvent;

public class State {
	private boolean finalState;
	private ArrayList<Transition> transitions;
	private ArrayList<IEvent> toDo;
	@SuppressWarnings("unused")
	private Automate automate;
	
	
	public State(Automate automate) {
		this.automate=automate;
		finalState=false;
	}
	
	public ArrayList<Transition> getTransitions() {
		if (transitions==null) transitions=new ArrayList<Transition>();
		return transitions;
	}
	
	public void addTransition(Transition t) {
		this.getTransitions().add(t);
	}
	
	public void setTodo(ArrayList<IEvent> toDo) {
		this.toDo = toDo;
	}
	
	public ArrayList<IEvent> getToDo() {
		if (toDo==null) toDo=new ArrayList<IEvent>();
		return toDo;
	}

	public boolean isFinalState() {
		return finalState;
	}

	public void setFinalState(boolean finalState) {
		this.finalState = finalState;
	}

	public State process(IEvent event) {
		State toReturn = null;
		
		for (Transition t : transitions) {
			if (t.getTemplate().accept(event)) {
				toReturn = t.getState();
				
//				if (toReturn.isFinalState()) {
//					automate.addOutgoingEvents(toReturn.getToDo());
//					toReturn=null;
//				}
				
			}
		}
		return toReturn;
	}	
	

}
